#!/usr/bin/env python
#go2_keymap.py

# convert if/else loop to functions
#requires gtk, but should be included at top level, not here

import gtk

class Go2_Keyfunc:
	def __init__(self, m,v, settings):
		self.v = v
		self.m = m
		
		
			
	def load_event(self, event=None):
		self.event = event
		#useful for all functions - get path to selected item
		self.selection = self.v.treeview.get_selection()
		self.treestore, self.treeiter = self.selection.get_selected()
		if (self.treeiter is None):
			self.treeiter = self.treestore.get_iter((0,))
		self.path = self.m.get_path(self.treeiter)
		self.expanded = self.v.treeview.row_expanded(self.path)
		if(self.event):
			self.ctrl= self.shift=self.special= self.alt = ''
			#flag special modifier keys
			special=ctrl=alt=shift=0
			if event.state & gtk.gdk.CONTROL_MASK:
				print "Control was being held down"
				self.ctrl=1
				self.special=1
			if event.state & gtk.gdk.MOD1_MASK:
				print "Alt was being held down"
				self.alt=1
				self.special=1
			if event.state & gtk.gdk.SHIFT_MASK:
				print "Shift was being held down"
				self.shift=1
				self.special=1
		
	def map_Right(self):
		if(self.ctrl):
				return(True)
		if self.v.editflag != True:
			self.v.treeview.expand_row( self.path, True )
		return False #true stops key propagation any further
		
	def map_Left(self):
		if(self.ctrl):
				return(True)
		if self.v.editflag != True:
			self.v.treeview.collapse_row( self.path )
		return False
		
	def map_Up(self):
		if(self.ctrl):
			self.v.move_up(self.treestore, self.treeiter)
			return True
	def map_Down(self):
		if(self.ctrl):
			self.v.move_down(self.treestore, self.treeiter)
			return True
	def map_x(self):
		parent = self.treestore.iter_parent(self.treeiter)
		print self.path
		print parent
		print self.treeiter
		self.treestore.remove(self.treeiter)
		# now select previous sibling, or if none select parent
		
	def map_q(self):
		if(self.ctrl):
			gtk.main_quit()
			
	def map_o(self):
		if(self.ctrl):
			pass # do not have access to controller functions!
			
	def map_Delete(self):
		if (self.treestore.get_path(self.treestore.get_iter_root()) != self.path):
			previous_sib = self.v.get_sibling_previous_iter(self.treestore, self.treeiter)
			if(previous_sib):
				self.v.focus(self.treestore, previous_sib)
			else:
				#no previous sibling, check for parent
				parent = self.treestore.iter_parent(self.treeiter)
				if(parent):
					self.v.focus(self.treestore, parent)
			self.treestore.remove(self.treeiter)
			
		else:
			self.treestore[self.path][0] = ''
	
	def map_x(self):
		if(self.ctrl):
			self.treestore[self.path][0] = ''
			return True
			
	def map_Return(self):

		if(self.ctrl):
			print "hit return key w/ctrl"
			#finish editing
			self.v.save_and_stop_editing()

		else:		
			print "hit return key"
			
			if(self.v.editflag):
				#save buffer contents into current node
				self.v.save_and_stop_editing()
			#create new node
			parent = self.treestore.iter_parent(self.treeiter)
			newiter = self.treestore.insert_after(parent,self.treeiter)
			newpath = self.treestore.get_path(newiter)
			self.v.treeview.set_cursor_on_cell(newpath, self.v.tvcolumn, self.v.cell, True)
		
		
	def map_Tab(self):
		print "tab, no special - indenting"
		if(self.v.editflag == True):
			self.v.save_and_stop_editing()
		
		self.v.indent(self.treestore, self.treeiter)
		
	def map_ISO_Left_Tab(self):
		print "shift Tab - outdenting"
		if(self.v.editflag == True):
			self.v.save_and_stop_editing()
		self.v.outdent(self.treestore, self.treeiter)
		
	def map_Escape(self):
		self.v.stop_editing()
		
		
	def map_doubleclick(self):
		if(self.expanded):
			print("expanded, collapsing!")
			self.v.treeview.collapse_row( self.path )
		else:
			print("collapsed, expanding!")
			self.v.treeview.expand_row( self.path, False )
			
	def map_default(self):
		# this is the default case, whenever user types, editing should begin
		if(not self.v.editflag):
			self.v.treeview.set_cursor_on_cell(self.path, self.v.tvcolumn, self.v.cell, True)

				
	
